/*
 * Alloy Analyzer
 * Copyright (c) 2002 Massachusetts Institute of Technology
 *
 * The Alloy Analyzer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * The Alloy Analyzer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Alloy Analyzer; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package alloy.ast;

import alloy.type.RelationType;
import alloy.type.UnionType;

/**
 * Alloy Analyzer
 * Copyright (c) 2001
 * Massachusetts Institute of Technology
 *
 * Class: SetMultExpr
 * owner: brianlin / msridhar / emina
 * Interface status: unstable
 * Implementation status: unstable
 *
 * AST node representing a/an SetMultExpr:  Multiplicity Expr
 */

public class SetMultExpr extends TreeNode implements MultiplicityExpr {

    /**
     * index of mult in child list
     */
    private static final int MULT_INDEX = 0;

    /**
     * index of expr in child list
     */
    private static final int EXPR_INDEX = 1;

   
    /** the {@link RelationType} of this expression */
    private UnionType _type;

    public SetMultExpr(Location location_, Multiplicity mult_, Expr expr_) {
        super(location_, mult_, expr_);
	setType(expr_.getType());
    }


    public SetMultExpr(Multiplicity mult_, Expr expr_) {
        this(Location.UNKNOWN, mult_, expr_);
    }



    //GET AND SET METHODS

    /**
     * gets the expr of this
     * @return the {@link Expr}
     */
    public Expr getExpr() {
        return (Expr)childAt(EXPR_INDEX);
    }

    /** set the Expr of this */
    public void setExpr(Expr newChild_) {
        setChild(EXPR_INDEX, newChild_);
	setType(newChild_.getType());
    }

    public Expr toExpr() {
        return (Expr)childAt(EXPR_INDEX).copy();
    }
    
    /**
     * gets the setMult of this
     * @return the {@link Multiplicity}
     */
    public Multiplicity getMult() {
        return (Multiplicity)childAt(MULT_INDEX);
    }

    /** set the mult of this */
    public void setMult(Multiplicity newChild_) {
        setChild(MULT_INDEX, newChild_);
    }

    public boolean isSameAs(MultiplicityExpr multExpr_) {
	if (!multExpr_.getClass().equals(getClass())) return false;
	SetMultExpr setMultExpr_ = (SetMultExpr)multExpr_;
	return
	    _type.equals(setMultExpr_._type) &&
	    getMult().equals(setMultExpr_.getMult());
    }

    /**
     * get the type of this
     * @return the type of this
     */
    public UnionType getType() {
        return _type;
    }
    
    /**
     * set the type of this
     * @param type_ new type for this
     */
    public void setType(UnionType type_) {
        _type = type_;
        if (type_ != null) {
            descr = "type " + type_.toString();
        } else {
            descr = "";
        }
    }

    public Object copy() {
        SetMultExpr exprCopy = (SetMultExpr)super.copy();
        exprCopy.setType(_type);
        return exprCopy;
    }
    
    /**
     * accept an AST visitor
     * @param visitor_ the visitor
     */
    protected void acceptVisitor(ASTVisitor visitor_) {
        visitor_.visit(this);
    }

    /**
     * accept a returning AST visitor
     * @param visitor_ the visitor
     * @return the Object returned by the visitor
     */
    protected Object acceptReturnVisitor(ASTReturnVisitor visitor_) {
        return visitor_.visit(this);
    }

    public String toString()
    {
        return nodeString();
    }

}
